package com.sxwl.auth.service;

import com.sxwl.auth.mapper.ChatMessageMapper;
import com.sxwl.auth.mapper.ChatRecordMapper;
import com.sxwl.auth.mapper.OnlineUserMapper;
import com.sxwl.auth.netty.UserChannelMap;
import com.sxwl.dto.ChatMessageDTO;
import com.sxwl.dto.ChatMessageUserDTO;
import com.sxwl.exception.MyException;
import com.sxwl.pojo.ChatMessage;
import com.sxwl.pojo.ChatRecord;
import com.sxwl.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author reading
 * @Description TODO
 * @Date 2020/4/14 16:53
 */
@Service
@Slf4j
public class ChatInfoService {
    @Autowired
    private OnlineUserMapper onlineUserMapper;
    @Autowired
    private ChatRecordMapper chatRecordMapper;
    @Autowired
    private ChatMessageMapper chatMessageMapper;


    /**
     * 获取在线好友
     * @param id
     * @return
     */
    public List<User>   getOnlineUser(String id) {
//        User user = onlineUserMapper.selectByPrimaryKey(id);//查询用户
        List<String> onlineUser = UserChannelMap.getOnlineUser(id);
        if(CollectionUtils.isEmpty(onlineUser)){
            return   Lists.newArrayList();
        }
        List<User> users = onlineUserMapper.selectByIdList(onlineUser);
        if(CollectionUtils.isEmpty(users)){
          return   Lists.newArrayList();
        }
        return users;
    }
    /**
     * 查询聊天记录
     */
    public List<ChatRecord> findChatRecord(Integer id,Integer friendid){
        Example example=new Example(ChatRecord.class);
        Example.Criteria criteria = example.createCriteria();
        Example.Criteria criteria1 = example.createCriteria();

        criteria.andEqualTo("userid",id);
        criteria.andEqualTo("friendid",friendid);

        criteria1.andEqualTo("userid",friendid);
        criteria1.andEqualTo("friendid",id);

        example.or(criteria);
        example.or(criteria1);

        List<ChatRecord> chatRecords = chatRecordMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(chatRecords)){
            return   Lists.newArrayList();
        }
        return chatRecords;
    }

    /**
     * 保存聊天记录
     */
    public void saveRecord(ChatRecord chatRecord){
        chatRecordMapper.insertSelective(chatRecord);
    }
    /**
     *查询聊天消息
     */
    public ChatMessageDTO selectCharMessage(String id) {
        List<ChatMessage> chatMessages = findChatMessage(id, id);
        if(CollectionUtils.isEmpty(chatMessages)){
            return  null ;
        }
        ChatMessageDTO chatMessageDTO=new ChatMessageDTO();
        chatMessages.forEach(item->{
            /**
             * 查询未读消息
             */
            if(StringUtils.isBlank(item.getUserid())||StringUtils.isBlank(item.getFriendid())){
                log.error("用户信息异常");
                throw new MyException("用户信息异常");
            }
            ChatRecord chatRecord = chatRecordMapper.getChatRecord(item.getUserid(), item.getFriendid());
            ChatMessageUserDTO chatMessageUserDTO=new ChatMessageUserDTO();
            chatMessageUserDTO.setChatRecord(chatRecord);
            String key;
            if(item.getUserid().equals(id)){
                key=item.getFriendid();
            }else{

                key=String.valueOf(item.getUserid());
            }
            User user = onlineUserMapper.selectByPrimaryKey(key);
            //查询所有未读消息
            Integer hasReadMessage = getHasReadMessage(Integer.valueOf(user.getId()), Integer.valueOf(id));
            chatMessageUserDTO.setHasRead(hasReadMessage);//设置未读消息数量
            chatMessageUserDTO.setUser(user);
        chatMessageDTO.getChatMessages().add(chatMessageUserDTO);
        });
        return chatMessageDTO;
    }

    /**
     * 保存聊天消息
     */
    public void saveChatMessage(ChatMessage chatMessage){
        if(chatMessage!=null){
            chatMessageMapper.insertSelective(chatMessage);
        }
    }

    /**
     * 查询聊天消息
     */
    public List<ChatMessage> findChatMessage(String userid,String friendid){
        Example example=new Example(ChatMessage.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("userid",userid);
        criteria.orEqualTo("friendid",friendid);
        List<ChatMessage> chatMessages = chatMessageMapper.selectByExample(example);
        return  chatMessages;
    }

    /**
     * 查询用户未读消息
     */
    public Integer getHasReadMessage(Integer userid, Integer friendid){
        Integer hasReadMesage = chatMessageMapper.getHasReadMesage(userid, friendid);
        return hasReadMesage;
    }


    /**
     * 判断是否保存
     */
    public List<ChatMessage> isSave(String userid,String friendid){
        Example example=new Example(ChatRecord.class);
        Example.Criteria criteria = example.createCriteria();
        Example.Criteria criteria1 = example.createCriteria();

        criteria.andEqualTo("userid",userid);
        criteria.andEqualTo("friendid",friendid);

        criteria1.andEqualTo("userid",friendid);
        criteria1.andEqualTo("friendid",userid);

        example.or(criteria);
        example.or(criteria1);
        List<ChatMessage> chatMessages = chatMessageMapper.selectByExample(example);
        return  chatMessages;
    }
    /**
     * 设置消息未已读
     */
    public void setMessageHasRead(Integer userid,Integer friendid){
        chatMessageMapper.setMeeageRead(userid,friendid);
    }
}
